//
//  SPPreferencesController.m
//  Sophie
//
//  Created by Anton Chebotaev on 4/11/09.
//  Copyright 2009 IFMO. All rights reserved.
//

#import "SPPreferencesController.h"

// Constants
NSString * const SPPreferencesControllerDefaultUsername = @"Livejournal username";

NSString * const SPPreferencesControllerShouldCreateDocumentOnStart = @"Create Document on programm startup";
NSString * const SPPreferencesControllerShouldDetectMusicForNewEntry = @"Detect music for each new entry";
NSString * const SPPreferencesControllerDefaultPrivacyLevel = @"Default Privacy Level";
NSString * const SPPreferencesControllerDefaultPrivacyGroupHash = @"Default Privacy Group Hash";
NSString * const SPPreferencesControllerShouldUseAutosave = @"Should Use Autosave";
NSString * const SPPreferencesControllerDefaultAutosaveInterval = @"Autosave interval (minutes)";
NSString * const SPPreferencesControllerShouldCheckForUpdates = @"Should Check for updates";
NSString * const SPPreferencesControllerDefaultUpdatesCheckInterval = @"Check for updates interval (daya)";
NSString * const SPPreferencesControllerLastUpdateCheckDate = @"Last update check date";
NSString * const SPPreferencesControllerMusicFormat = @"Music Format";


@interface SPPreferencesController (Private)
- (NSObject *)getPropertyNamed:(NSString *)propertyName defaultValue:(NSObject *)aValue;
- (void)setPropetryNamed:(NSString *)propertyName value:(NSObject *)aValue;
@end

#pragma mark -
@implementation SPPreferencesController

#pragma mark Life Cycle
- (id)init {
	if (self = [super init]) {
		// initializing mebers and adding observers
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

static SPPreferencesController *sharedPreferencesController = nil;
+ (SPPreferencesController *)sharedController {
	@synchronized(self) {
		if (sharedPreferencesController == nil) {
			[[self alloc] init]; // assignment not done here //NOTE:Why?
		}
	}
	return sharedPreferencesController;
}

+ (id)allocWithZone:(NSZone *)zone {
	@synchronized(self) {
		if (sharedPreferencesController == nil) {
			sharedPreferencesController = [super allocWithZone:zone];
			return sharedPreferencesController;
		}
	}
	return nil; // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone {
	return self;
}

- (id)retain {
	return self;
}

- (void)release {
	// do nothing
}

- (id)autorelease {
	return self;
}

#pragma mark Defaults

- (id)getPropertyNamed:(NSString *)propertyName defaultValue:(id)aValue{
	@synchronized(self) {
		id value = [[NSUserDefaults standardUserDefaults] objectForKey:propertyName];
		if (value) {
			return value;
		} else {
			[self setPropetryNamed:propertyName value:aValue];
			return aValue;
		}
	}
	return nil;
}
- (void)setPropetryNamed:(NSString *)propertyName value:(id)aValue {
	@synchronized(self) {
		[[NSUserDefaults standardUserDefaults] setObject:aValue forKey:propertyName];
	}	
}


- (NSString *)defaultUsername {
	return (NSString *)[self getPropertyNamed:SPPreferencesControllerDefaultUsername
								 defaultValue:@""];
}
- (void)setDefaultUsername:(NSString *)aUsername {
	[self setPropetryNamed:SPPreferencesControllerDefaultUsername
					 value:aUsername];
}

- (BOOL)shouldCreateDocumentOnStart {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerShouldCreateDocumentOnStart
   								  defaultValue:[NSNumber numberWithBool:YES]] boolValue];
}
- (void)setShouldCreateDocumentOnStart:(BOOL)aValue {
	[self setPropetryNamed:SPPreferencesControllerShouldCreateDocumentOnStart
					 value:[NSNumber numberWithBool:aValue]];
}

- (BOOL)shouldDetectMusicForNewEntry {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerShouldDetectMusicForNewEntry
   								  defaultValue:[NSNumber numberWithBool:NO]] boolValue];
}
- (void)setShouldDetectMusicForNewEntry:(BOOL)aValue {
	[self setPropetryNamed:SPPreferencesControllerShouldDetectMusicForNewEntry
					 value:[NSNumber numberWithBool:aValue]];
}

- (int)defaultPrivacyLevel {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerDefaultPrivacyLevel
   								  defaultValue:[NSNumber numberWithInt:LJPublicEntry]] intValue];
}
- (void)setDefaultPrivacyLevel:(int)aValue {
	[self setPropetryNamed:SPPreferencesControllerDefaultPrivacyLevel
					 value:[NSNumber numberWithInt:aValue]];
}

- (int)defaultPrivacyGroupHash {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerDefaultPrivacyGroupHash
   								  defaultValue:[NSNumber numberWithInt:0]] intValue];
}
- (void)setDefaultPrivacyGroupHash:(int)aValue {
	[self setPropetryNamed:SPPreferencesControllerDefaultPrivacyGroupHash
					 value:[NSNumber numberWithInt:aValue]];
}

- (BOOL)shouldUseAutosave {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerShouldUseAutosave
   								  defaultValue:[NSNumber numberWithBool:YES]] boolValue];
}
- (void)setShouldUseAutosave:(BOOL)aValue {
	[self setPropetryNamed:SPPreferencesControllerShouldUseAutosave
					 value:[NSNumber numberWithBool:aValue]];
}

- (int)defaultAutosaveInterval {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerDefaultAutosaveInterval
   								  defaultValue:[NSNumber numberWithInt:0]] intValue];
} 
- (void)setDefaultAutosaveInterval:(int)aValue {
	[self setPropetryNamed:SPPreferencesControllerDefaultAutosaveInterval
					 value:[NSNumber numberWithInt:aValue]];
}


- (BOOL)shouldCheckForUpdates {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerShouldCheckForUpdates
   								  defaultValue:[NSNumber numberWithBool:YES]] boolValue];
}
- (void)setShouldCheckForUpdates:(BOOL)aValue {
	[self setPropetryNamed:SPPreferencesControllerShouldCheckForUpdates
					 value:[NSNumber numberWithBool:aValue]];
}

- (int)defaultUpdatesCheckInterval {
	return [(NSNumber *)[self getPropertyNamed:SPPreferencesControllerDefaultUpdatesCheckInterval
   					              defaultValue:[NSNumber numberWithInt:0]] intValue];
}
- (void)setDefaultUpdatesCheckInterval:(int)aValue {
	[self setPropetryNamed:SPPreferencesControllerDefaultUpdatesCheckInterval
					 value:[NSNumber numberWithInt:aValue]];
}

- (NSDate *)lastUpdateCheck {
	return (NSDate *)[self getPropertyNamed:SPPreferencesControllerLastUpdateCheckDate
					           defaultValue:[NSDate date]];
}
- (void)setLastUpdateCheck:(NSDate *)aValue {
	[self setPropetryNamed:SPPreferencesControllerLastUpdateCheckDate
					 value:aValue];
}

- (NSArray *)musicFormat {
	NSArray *defaultFormat = [NSArray arrayWithObjects:
							  [SPMusicFormatToken tokenWithString:@"♪ "],
							  [SPMusicFormatToken titleToken],
							  [SPMusicFormatToken tokenWithString:@" — "],
							  [SPMusicFormatToken titleToken],
							  nil];
	
	NSData *defaultData = [NSKeyedArchiver archivedDataWithRootObject:defaultFormat];
	NSData *musicFormatAsData = (NSData *)[self getPropertyNamed:SPPreferencesControllerMusicFormat
													defaultValue:defaultData];
    return [NSKeyedUnarchiver unarchiveObjectWithData:musicFormatAsData];
}
- (void)setMusicFormat:(NSArray *)format {
	NSData *musicFormatAsData = [NSKeyedArchiver archivedDataWithRootObject:format];
	[self setPropetryNamed:SPPreferencesControllerMusicFormat
					 value:musicFormatAsData];
}

@end
